home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 06 - 1990 / 06.11 Nov 90 / NeuralNet Estimator⁄EDIT / Setup Network.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-08-03  |  2.6 KB  |  110 lines  |  [TEXT/KAHL]

  1. #include "Neural Network.h"
  2. #include <math.h>
  3.  
  4. extern NeuralNet *    theNet;
  5. extern DTypeVector     yData;
  6. extern DTypeMatrix    XData;
  7. extern DTypeVector     Alpha[];
  8. extern DTypeMatrix  Jac_T;
  9. extern DTypeVector     Pi, Diag;
  10. extern DTypeVector     Resid;
  11. extern DTypeVector  gradSS;
  12. extern DTypeVector     dSquash[];
  13. extern DTypeMatrix  Phi, T2;
  14.  
  15. double    CalcMachEPS();
  16.  
  17. /***************************************************************************/
  18. SetupNetDefaults()
  19. {    
  20.     SetupTolerances();
  21.     theNet->OutLayer = 2;
  22.     theNet->Units[0] = 0;
  23.     theNet->Units[1] = 2;            /* defaults to two units in first hidden layer */
  24.     theNet->itnlimit = 15;
  25.     theNet->maxstep = 10.0;            /* default step limit */
  26.     theNet->maxrelstep = .1;        /* default of a 10% max relative step, not used by default HookeJeeves method */
  27.     theNet->usemaxstep = TRUE;        /* force use of max step limit, not used by HookeJeeves method */
  28.     theNet->type = SigUB;            /* defaults to unbiased Sigma type network */
  29.     theNet->method = HookeJeeves;    /* defaults to Hooke & Jeeves direct search method */
  30.     theNet->squash = Logistic;
  31.     theNet->valid = FALSE;            /* new net is not estimated yet */
  32. }
  33.  
  34. /*------------------
  35.     Allot memory for weight matrices and initialize weight values.  Expects the number
  36.     of units in each layer to be already defined.
  37. */
  38. AllotInitNewNetWeights()
  39. {
  40.     int i;
  41.     
  42.     for(i=0; i<theNet->OutLayer; i++) /* output layer has no weight matrix */
  43.     {    /* ---- allot memory for Weight matrices ----*/
  44.         AllotDTypeMatrix(&(theNet->W[i]), theNet->Units[i+1], theNet->Units[i]);
  45.                     /* for layer i:
  46.                          number of columns is number of neurons in layer i,
  47.                          number of rows is number of neurons in layer i+1
  48.                     */
  49.     
  50.         /*---- initialize parameter values ----*/
  51.         HLock(theNet->W[i].cells);
  52.         InitWeights(&(theNet->W[i]));
  53.         HUnlock(theNet->W[i].cells);
  54.     }
  55. }
  56.  
  57. HLockNet()
  58. {
  59.     int i;
  60.     
  61.     for(i=0; i<theNet->OutLayer; i++)
  62.     {    HLock(theNet->W[i].cells);
  63.     }
  64. }
  65.  
  66. HUnlockNet()
  67. {
  68.     int i;
  69.     
  70.     for(i=0; i<theNet->OutLayer; i++)
  71.     {    HUnlock(theNet->W[i].cells);
  72.     }
  73. }
  74.  
  75. DisplayNet()
  76. {
  77.     int i;
  78.     printf("Output layer is %d \n",theNet->OutLayer);
  79.     for(i=0; i<=theNet->OutLayer; i++)
  80.         printf("Neurons in layer %d is %d\n",i,theNet->Units[i]);
  81.     for(i=0; i<theNet->OutLayer; i++)
  82.     {
  83.         printf("Weight matrix for layer %d is:\n",i);
  84.         DisplayDTypeMatrix(&theNet->W[i]);
  85.     }
  86. }
  87.  
  88. InitWeights(matrix)
  89.     DTypeMatrix * matrix;    /* weight matrix to use in network calculation    */
  90. {
  91.     int    i,j,k;
  92.     DataType * cell;
  93.     DataType d = .10;
  94.  
  95.     cell = *matrix->cells;
  96.     for(i=0, k=0; i<matrix->rows; i++)    
  97.     {                        
  98.         for(j=0; j<matrix->cols; j++, k++, d = d+.01) 
  99.             *(cell + (i*matrix->cols + j)) = d*(DataType)pow(-1,k);
  100.     }
  101.     
  102. }
  103.  
  104. SetupTolerances()
  105. {
  106.     theNet->gradtol = .000001;
  107.     theNet->steptol = .000000000001;
  108. }
  109.  
  110.